#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>

#include <rpc/rpc.h>

#include "ndmp.h"
#include "err.h"

int open_tapedrive(struct ndmp_session_detail *nh, char *name)
{

	int i;

	if ((i=init_ndmp_handle(nh))) return i;
	if ((i=do_ndmp_authenticate(nh))) return i;
        if ((i=device_open (nh, name , NDMP_SCSI_OPEN, 0))) return i;
        if ((i=device_close (nh, NDMP_SCSI_CLOSE))) return i;
	shutdown(nh->s,2);
	return 0;
}

int list_devices(struct ndmp_session_detail *nh) 
{


        ndmp_config_get_device_info_reply device_info;
	ndmp_config_get_host_info_reply host_info;
        int i,j,x;
	int s;

	if ((i=init_ndmp_handle(nh))) return i;
	if ((i=do_ndmp_authenticate(nh))) return i;
	if ((i=get_host_info(nh,&host_info))) return i;

	printf("\nNDMP Server details:\n");
	printf("--------------------\n");
	printf("Host Name: %s\n", host_info.hostname);
	printf("OS Type: %s\n", host_info.os_type);
	printf("OS Version: %s\n", host_info.os_vers);
	printf("Host id: %s\n", host_info.hostid);

	printf("\nTape devices:\n");
	printf("-------------");
	if ((s=get_device_info( nh, & device_info,NDMP_CONFIG_GET_TAPE_INFO))) return s;

        for(i=0; i<device_info.scsi_info.scsi_info_len; i++ ) {

          printf("\nModel: %s ", device_info.scsi_info.scsi_info_val[i].model);

          for (j=0;j<device_info.scsi_info.scsi_info_val[i].caplist.caplist_len;j++) {
              printf(" / %s", device_info.scsi_info.scsi_info_val[i].caplist.caplist_val[j].device);
              printf(" / %d", device_info.scsi_info.scsi_info_val[i].caplist.caplist_val[j].attr);
              for(x=0;x<device_info.scsi_info.scsi_info_val[i].caplist.caplist_val[j].capability.capability_len;x++) {

                      printf(" / %s : ", device_info.scsi_info.scsi_info_val[i].caplist.caplist_val[j].capability.capability_val[x].name);
                      printf("%s", device_info.scsi_info.scsi_info_val[i].caplist.caplist_val[j].capability.capability_val[x].value);
              }
          }
        }

	printf("\n\nRobot devices:\n");
	printf("--------------\n");
	s=get_device_info( nh, & device_info,NDMP_CONFIG_GET_SCSI_INFO);

        printf("Get Library/Robot informnation : %s",errorp(s));


        for(i=0; i<device_info.scsi_info.scsi_info_len; i++ ) {

          printf("\nModel: %s ", device_info.scsi_info.scsi_info_val[i].model);

          for (j=0;j<device_info.scsi_info.scsi_info_val[i].caplist.caplist_len;j++) {
              printf(" / %s", device_info.scsi_info.scsi_info_val[i].caplist.caplist_val[j].device);
              printf(" / %d", device_info.scsi_info.scsi_info_val[i].caplist.caplist_val[j].attr);
              for(x=0;x<device_info.scsi_info.scsi_info_val[i].caplist.caplist_val[j].capability.capability_len;x++) {

                      printf(" / %s : ", device_info.scsi_info.scsi_info_val[i].caplist.caplist_val[j].capability.capability_val[x].name);
                      printf("%s", device_info.scsi_info.scsi_info_val[i].caplist.caplist_val[j].capability.capability_val[x].value);
              }
          }
        }
	printf("\n");
	shutdown(nh->s,2);
	return 0;

}
